import React from 'react'
import { View, StyleSheet, Text } from 'react-native'
import { connect } from 'react-redux'
import { ScaleS, ScaleT, ONE_PIXEL, WIDTH } from 'src/utils/ScreenUtil'
import CommunicationCell from './components/communication-cell/index'
import RefreshState from '../../../components/custom-flat-list/RefreshState'
import { deleteChatByIds, getChatForCList, myCenterNumbers } from '../../../utils/RequestAPI'
import { CommonToast, DismissLoading, ShowLoading } from '../../../utils/ToastUtil'
import EmptyView from '../../../components/empty-view/index'
import { UPDATE_MY_CENTER_NUMBERS_ACTION } from '../../mine/mine-page/store/action'
import CommonSelectDelete from '../../../pageP/common/common-select-delete/index'
// 发送行为
const mapDispatchToProps = dispatch => ({
  updateMyCenterNumbers (payload) {
    dispatch(UPDATE_MY_CENTER_NUMBERS_ACTION(payload))
  }
})

@connect(({ userinfo, CMineCenterStore, landmark }) => ({
  userinfoC: userinfo.C,
  myCenterNumbers: CMineCenterStore.myCenterNumbers,
  landmark
}), mapDispatchToProps)
class CCommunicationHistory extends CommonSelectDelete {
  constructor (props) {
    super(props)
    this.state = {
      ...this.state,
      dataArr: [],
      titleNum: props.myCenterNumbers.talentTalk || 0
    }
    this.titleText = '沟通历史'
    this.deleteText = '沟通历史'
    this.page = 1
  }

  componentDidMount () {
    this.onHeaderRefresh()
  }

  generateItemChild (itemParams) {
    const { item } = itemParams
    const basicInfo = JSON.parse(item.basicInfo)
    const jobItem = {
      ...item,
      gmtCreate: item.gmtCreat,
      basicInfoModel: {
        name: basicInfo.name,
        settlementMethod: basicInfo.settlementMethod,
        salary: basicInfo.salary,
        category: basicInfo.category,
        practiceLabel: basicInfo.practiceLabel,
        type: basicInfo.type
      },
      postWorkLocations: [{
        address: item.address[0]
      }]

    }
    console.log(jobItem)
    return (
      <CommunicationCell item={jobItem} landmark={this.props.landmark} isCollection={true}/>
    )
  }

  childItemClick (itemParams) {
    const { item } = itemParams
    console.info(item)
    this.props.navigation.navigate('CJobdetail', {
      id: item.postId
    })
  }

  listEmptyComponent () {
    return <EmptyView
      emptyImg={require('src/imgC/userinfo/communication-history-null.png')}
      firstTip={'无沟通历史'}
      secondTip={'快去和人才交流吧～'}/>
  }

  onHeaderRefresh () {
    this.page = 1
    this._postCollectionList(true).then((datas) => {
      this.setState(() => ({
        dataArr: datas
      }), () => {
        this._refreshState(datas)
      })
    })
  }

  onFooterRefresh () {
    ++this.page
    this._postCollectionList().then((datas) => {
      this.setState(() => ({
        dataArr: this.state.dataArr.concat(datas)
      }), () => {
        this._refreshState(datas)
      })
    })
  }

  _postCollectionList = (showLoading) => {
    showLoading && ShowLoading()
    const { userinfoC } = this.props
    return new Promise((resolve) => {
      getChatForCList({
        params: {
          page: this.page,
          limit: 10
        },
        headers: {
          cmdUserId: userinfoC.userId
        }
      }).then((data) => {
        showLoading && DismissLoading()
        console.info(data)
        resolve(data.data)
      }).catch(() => {
        showLoading && DismissLoading()
      })
    })
  }

  _refreshState = (datas) => {
    console.log(this.flatListRef)
    if (this.flatListRef) {
      if (datas.length > 9) {
        this.flatListRef.endRefreshing(RefreshState.CanLoadMore)
      } else {
        this.flatListRef.endRefreshing(RefreshState.NoMoreData)
      }
    }
  }

  confirmDelete () {
    ShowLoading()
    const { dataArr, checkedIndexes } = this.state
    const { updateMyCenterNumbers, userinfoC } = this.props
    const idArr = dataArr.reduce((idArr, currentValue, index) => {
      return checkedIndexes.includes(index) ? idArr.concat(currentValue.id) : idArr
    }, [])
    deleteChatByIds({
      params: {
        type: 'C',
        ids: idArr.join(',')
      }
    }).then((data) => {
      DismissLoading()
      CommonToast(`${checkedIndexes.length}条沟通历史删除成功`)
      myCenterNumbers({
        params: {
          type: 'C',
          userId: userinfoC.userId
        }
      }).then((data) => {
        this.setState({
          titleNum: data.data.talentTalk
        })
      })
      this.onHeaderRefresh()
      this.setState({
        checkedIndexes: []
      })
    }).catch(() => {
      DismissLoading()
    })
  }
}

export default CCommunicationHistory

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  managerView: {
    width: ScaleS(58),
    height: ScaleS(28),
    borderRadius: ScaleS(14),
    alignItems: 'center',
    justifyContent: 'center',
    borderColor: '#000000',
    borderWidth: ScaleS(1),
    borderStyle: 'solid'
  },
  managerText: {
    color: '#000000',
    fontSize: ScaleT(12),
    fontWeight: '600'
  },
  titleText: {
    color: '#444444',
    fontSize: ScaleT(25),
    fontWeight: '600',
    marginTop: ScaleS(30),
    marginLeft: ScaleS(20),
    marginBottom: ScaleS(30)
  },
  tagImg: {
    marginLeft: ScaleS(4),
    width: ScaleS(18),
    height: ScaleS(21),
    resizeMode: 'cover'
  },
  line: {
    height: ONE_PIXEL,
    backgroundColor: '#EFEFEF'
  },
  flatList: {
    flex: 1
  },
  itemView: {
    height: ScaleS(122),
    flexDirection: 'row'
  },
  selectView: {
    paddingTop: ScaleS(38),
    paddingLeft: ScaleS(20)
  },
  selectIconView: {
    width: ScaleS(20),
    height: ScaleS(20),
    borderColor: '#979797',
    borderRadius: ScaleS(10),
    borderWidth: ScaleS(1),
    borderStyle: 'solid'
  },
  selectedIconView: {
    width: ScaleS(22),
    height: ScaleS(22),
    backgroundColor: '#000000',
    borderRadius: ScaleS(11),
    justifyContent: 'center',
    alignItems: 'center'
  },
  selectedIconImg: {
    width: ScaleS(12),
    height: ScaleS(8)
  },
  itemContentView: {
    width: WIDTH,
    height: ScaleS(122),
    paddingHorizontal: ScaleS(20),
    paddingTop: ScaleS(35)
  },
  topView: {
    flexDirection: 'row'
  },
  jobTitleView: {
    flexDirection: 'row',
    alignItems: 'center',
    flex: 1
  },
  jobTile: {
    color: '#000000',
    fontSize: ScaleT(18),
    fontWeight: '500',
    maxWidth: ScaleS(220)
  },
  priceView: {
    flexDirection: 'row',
    alignItems: 'flex-end'
  },
  price: {
    color: '#FFC61C',
    fontSize: ScaleT(24),
    fontWeight: 'bold',
    height: ScaleS(28),
    lineHeight: ScaleS(28)
  },
  priceUnit: {
    color: '#444444',
    fontSize: ScaleT(12),
    height: ScaleS(17),
    fontWeight: '500',
    lineHeight: ScaleS(17)
  },
  bottomView: {
    flexDirection: 'row',
    alignItems: 'center',
    marginTop: ScaleS(25),
    marginBottom: ScaleS(15)
  },
  tagView: {
    marginRight: ScaleS(16)
  },
  tagItemView: {
    paddingVertical: ScaleS(6),
    paddingHorizontal: ScaleS(16),
    borderRadius: ScaleS(16),
    backgroundColor: 'rgba(216,216,216,0.205)'
  },
  tagLabel: {
    color: '#999999',
    fontSize: ScaleT(12),
    fontWeight: '400'
  },
  dateText: {
    color: '#444444',
    fontSize: ScaleT(12),
    fontWeight: '400'
  },
  bottomSelectView: {
    height: ScaleS(56),
    flexDirection: 'row',
    alignItems: 'center',
    paddingHorizontal: ScaleS(20)
  },
  allSelectedText: {
    color: '#444444',
    fontSize: ScaleT(15),
    fontWeight: '400',
    marginLeft: ScaleS(11),
    flex: 1
  },
  deleteView: {
    width: ScaleS(72),
    height: ScaleS(40),
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#000000',
    borderRadius: ScaleS(4)
  },
  deleteText: {
    color: '#FFFFFF',
    fontSize: ScaleT(15),
    fontWeight: '400'
  }
})
